﻿@using System.Reflection;
@using YUCodeGen.Utils;
@model PropertyInfo[]
@{
    Layout = null;
    string classname = (string)ViewData["ClassName"];
    string cat = (string)ViewData["cat"];

    string compName = $"{classname.ToLower()}-{cat.ToLower()}";
    string compClassname = $"{classname}{PageUtil.Capitalize(cat)}";

}
import {Component,Input,Output} from '@@angular/core';
import { FormBuilder, FormControl, FormGroup, Validators ,ReactiveFormsModule } from '@@angular/forms';
import { @(classname)curdComponent } from './@(classname)-curd.component';
import { HttpClient } from '@@angular/common/http';
import { SysConfig } from '../System/SystemConfig';

@@Component({
    selector: '@compName',
    templateUrl:'@(compName).component.html',
    styleUrls:["@(compName).component.css"]
    })
    export class @(compClassname)Component{

    parentComponent:@(classname)curdComponent;
    constructor(private http: HttpClient) {}
    form=new FormGroup({

    @foreach (PropertyInfo pinfo in Model)
    {
        if (cat != "create" || PageUtil.IsCreate(pinfo))
        {
            @Html.Partial("_FormControl", pinfo)
        }
    }

    });

    //getters for fields

    @foreach (PropertyInfo pinfo in Model)
    {
        if (cat != "create" || PageUtil.IsCreate(pinfo))
        {
            var name = pinfo.Name;
            @:get @(name)() {return this.form.get('@name');}
        }
    }

    setValue(data)
    {
        this.form.patchValue(data);
    }

    createClick()
    {
        if (this.form.valid)
        {
            this.parentComponent.showDetailWithData(this.form.value);
        }
    }

    saveClick()
    {
    if(this.form.valid)
    {
        this.http.post(SysConfig.apiRoot+'/@(classname.ToLower())',this.form.value,SysConfig.httpOptions)
        .subscribe(resp => {
            console.log(resp);
        },
        errResp=>{
            console.log("http error:");
            console.log(errResp);
        });
    }
    }   //-- saveClick

    cancelClick()
    {
        this.parentComponent.childCancel(this);
    }   //-- cancelClick


    }
